package com.business_clean.ui.adapter.personnel;

import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.business_clean.R;
import com.business_clean.app.weight.CustomAvatarView;
import com.business_clean.data.mode.address.AddressListEntity;
import com.business_clean.data.mode.roster.RosterList;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import me.hgj.mvvmhelper.ext.LogExtKt;

public class SelectPersonnelAdapter extends BaseQuickAdapter<RosterList, BaseViewHolder> {
    private int isMultiple;

    private int position = -1; //只有单选的时候用到

    public SelectPersonnelAdapter(int isSelected) {
        super(R.layout.item_select_personnel);
        this.isMultiple = isSelected;
    }

    @Override
    protected void convert(@NotNull BaseViewHolder holder, RosterList item) {
        CustomAvatarView customAvatarView = holder.getView(R.id.iv_item_roster_avatar);
        customAvatarView.setAvatar(item.getAvatar(), item.getUser_name());
        customAvatarView.getAvatarText().setTextSize(16f);
        holder.setText(R.id.tv_item_roster_name, item.getUser_name());
//                .setText(R.id.tv_item_roster_phone, item.getMobile())

        if (isMultiple == 1 || isMultiple == 2) {
            if (isMultiple == 1) {//单选的时候才走这里
                if (position == getItemPosition(item)) {
                    holder.getView(R.id.iv_item_roster_select).setVisibility(View.VISIBLE);
                } else {
                    holder.getView(R.id.iv_item_roster_select).setVisibility(View.GONE);
                }
            } else {
                holder.getView(R.id.iv_item_roster_select).setVisibility(item.isChoice() ? View.VISIBLE : View.GONE);
            }
        } else {
            holder.getView(R.id.iv_item_roster_select).setVisibility(View.GONE);
        }


        if ("1".equals(item.getIs_out())) {//是否为外部人员，外部人员，不需要显示以下信息 只显示岗位名称
            if (!TextUtils.isEmpty(item.getJob_name())) {
                holder.setText(R.id.tv_item_desc, item.getJob_name());
            } else {
                holder.setText(R.id.tv_item_desc, "外部人员");
            }
        } else { //总部成员
            if ("1".equals(item.getIs_head_office())) {//是总部人员
                holder.setText(R.id.tv_item_desc, "总部成员");
            } else {//项目成员

                StringBuilder stringBuilder = new StringBuilder();
                if (!TextUtils.isEmpty(item.getSex_name())) {
                    stringBuilder.append(item.getSex_name());
                }
                if (!TextUtils.isEmpty(item.getJob_name())) {
                    stringBuilder.append(" / ").append(item.getJob_name());
                }
                if (!TextUtils.isEmpty(item.getAge())) {
                    stringBuilder.append(" / ").append(item.getAge()).append("岁");
                }
                if (!TextUtils.isEmpty(item.getWork_age())) {
                    stringBuilder.append(" / ").append(item.getWork_age() + "").append("年");
                }
                holder.setText(R.id.tv_item_desc, stringBuilder.toString());
            }
        }


    }


    /**
     * 看是否需要单选
     */
    public void updateItem(int position) {
        if (isMultiple == 1) {
            this.position = position;
        } else {
            if (getData().get(position).isChoice()) {
                getData().get(position).setChoice(false);
            } else {
                getData().get(position).setChoice(true);
            }
        }
        notifyDataSetChanged();
    }


    /**
     * 获取已经选中的List
     */
    public List<RosterList> getSelectData() {
        List<RosterList> lists = new ArrayList<>();
        if (isMultiple == 1) {//说明是单选
            lists.add(getData().get(position));
        } else {//多选
            for (RosterList data : getData()) {
                if (data.isChoice()) {
                    lists.add(data);
                }
            }
        }
        return lists;
    }

    /**
     * 反选状态
     *
     * @param uuids
     */
    public void dataInvert(String uuids) {
        if (!TextUtils.isEmpty(uuids)) {
            String[] dataArray = uuids.replaceAll("\n", "").split(",");
            for (String data : dataArray) {
                LogExtKt.logE("人员->" + data, "");
                for (RosterList rosterList : getData()) {
                    if (rosterList.getUuid().equals(data)) {
                        rosterList.setChoice(true);
                    }
                }
            }
        }
        notifyDataSetChanged();
    }
}
